package com.ggs.starbug;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncryptionTools {

    public static void main(String[] args) throws NoSuchAlgorithmException {
        String md5_1 = getMD5("test", 32);
        System.out.println(md5_1);

        String md5_2 = getMD5("test");
        System.out.println(md5_2);

        String sha1 = getSha1("test");
        System.out.println(sha1);

        String sha256 = getSha256("test");
        System.out.println(sha256);

        String sha512 = getSha512("test");
        System.out.println(sha512);

        String sha384 = getSha384("test");
        System.out.println(sha384);
    }

    protected static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static String getSha512(String text) throws OutOfMemoryError, NoSuchAlgorithmException {
        MessageDigest messagedigest = MessageDigest.getInstance("SHA-512");
        messagedigest.update(text.getBytes(StandardCharsets.UTF_8));
        return bufferToHex(messagedigest.digest());
    }

    public static String getSha384(String text) throws OutOfMemoryError, NoSuchAlgorithmException {
        MessageDigest messagedigest = MessageDigest.getInstance("SHA-384");
        messagedigest.update(text.getBytes(StandardCharsets.UTF_8));
        return bufferToHex(messagedigest.digest());
    }

    public static String getSha256(String text) throws OutOfMemoryError, NoSuchAlgorithmException {
        MessageDigest messagedigest = MessageDigest.getInstance("SHA-256");
        messagedigest.update(text.getBytes(StandardCharsets.UTF_8));
        return bufferToHex(messagedigest.digest());
    }

    public static String getMD5(String text) throws NoSuchAlgorithmException {
        MessageDigest messagedigest = MessageDigest.getInstance("MD5");
        messagedigest.update(text.getBytes(StandardCharsets.UTF_8));
        return bufferToHex(messagedigest.digest());
    }

    public static String getSha1(String text) throws NoSuchAlgorithmException {
        MessageDigest messagedigest = MessageDigest.getInstance("SHA-1");
        messagedigest.update(text.getBytes(StandardCharsets.UTF_8));
        return bufferToHex(messagedigest.digest());
    }

    private static String bufferToHex(byte bytes[]) {
        return bufferToHex(bytes, 0, bytes.length);
    }

    private static String bufferToHex(byte bytes[], int m, int n) {
        StringBuffer stringbuffer = new StringBuffer(2 * n);
        int k = m + n;
        for (int l = m; l < k; l++) {
            appendHexPair(bytes[l], stringbuffer);
        }
        return stringbuffer.toString();
    }

    private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
        char c0 = hexDigits[(bt & 0xf0) >> 4];
        char c1 = hexDigits[bt & 0xf];
        stringbuffer.append(c0);
        stringbuffer.append(c1);
    }

    /**
     * md5加密
     *
     * @param plainText - 加密字符
     * @param length    - 截取长度（最多32，建议32）
     * @return
     */
    public static String getMD5(String plainText, int length) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");//获取MD5实例
            md.update(plainText.getBytes());//此处传入要加密的byte类型值
            byte[] digest = md.digest();//此处得到的是md5加密后的byte类型值

            int i;
            StringBuilder sb = new StringBuilder();
            for (int offset = 0; offset < digest.length; offset++) {
                i = digest[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    sb.append(0);
                sb.append(Integer.toHexString(i));//通过Integer.toHexString方法把值变为16进制
            }
            return sb.toString().substring(0, length);//从下标0开始，length目的是截取多少长度的值
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
}
